package nc.bs.gsp.hf054404;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import nc.bs.medpub.pub.MedQueryDMO;
import nc.jdbc.framework.processor.BeanListProcessor;
import nc.vo.gsp.hf054404.BHGQRBVO;
import nc.vo.gsp.hf054404.BHGQRHVO;
import nc.vo.gsp.pub.GspConst;
import nc.vo.jcom.lang.StringUtil;
import nc.vo.pub.BusinessException;
import nc.vo.pub.CircularlyAccessibleValueObject;

public class GspBhgqrDMO extends MedQueryDMO {

	@Override
	protected String getBillType() {
		return GspConst.BHGQR;
	}

	@Override
	protected Class<?> getHeadVOClass() throws BusinessException {
		return BHGQRHVO.class;
	}

	@Override
	protected Class<?> getBodyVOClass() throws BusinessException {
		return BHGQRBVO.class;
	}

	public CircularlyAccessibleValueObject[] queryAllHeadData(String whereString) throws BusinessException {
		String strWhere = " nvl(gsp_bhgqr_h.dr,0)=0 ";
		if (!StringUtil.isEmptyWithTrim(whereString)) {
			strWhere += " and (" + whereString + ")";
		}
		StringBuffer sql = new StringBuffer();
		sql
				.append(
						"select distinct gsp_bhgqr_h.* from gsp_bhgqr_h gsp_bhgqr_h inner join gsp_bhgqr_b gsp_bhgqr_b on gsp_bhgqr_h.pk_bhgqr_h = gsp_bhgqr_b.pk_bhgqr_h")
				.append(" where ").append(strWhere);

		List rs = (ArrayList) getDao().executeQuery(sql.toString(), new BeanListProcessor(BHGQRHVO.class));
		if (rs != null && !rs.isEmpty()) {
			return (BHGQRHVO[]) rs.toArray(new BHGQRHVO[0]);
		}
		return null;
	}

	public CircularlyAccessibleValueObject[] queryAllBodyData(String key) throws BusinessException {
		return queryAllBodyData(key, null);
	}

	public CircularlyAccessibleValueObject[] queryAllBodyData(String key, String whereString) throws BusinessException {
		if (StringUtil.isEmptyWithTrim(key)) {
			return null;
		}
		StringBuffer sb = new StringBuffer("");
		sb.append(BHGQRHVO.PK).append("='").append(key).append("'");
		sb.append(" and nvl(dr,0)=0 ");
		if (!StringUtil.isEmptyWithTrim(whereString)) {
			sb.append(" and (").append(whereString).append(")");
		}
		Collection rs = getDao().retrieveByClause(BHGQRBVO.class, sb.toString());
		if (rs != null && !rs.isEmpty()) {
			return (BHGQRBVO[]) rs.toArray(new BHGQRBVO[0]);
		}
		return null;
	}

}
